const { Users } = require("../models/users");

//获得车主列表
async function getCarOwnersList(req, res) {
  const { role, inputVal, sear } = req.body;
  const data = await Users.find({ role });
  let newData = data;
  if (inputVal != "" && inputVal != null && inputVal != undefined) {
    newData = await Users.find({
      [sear]: { $regex: inputVal, $options: "$i" },
    });
    try {
      res.send({
        code: 1,
        data: newData,
        message: "获取车主成功",
      });
    } catch (error) {
      res.send({
        code: 0,
        message: "获取车主失败",
      });
    }
  } else {
    try {
      res.send({
        code: 1,
        data: data,
        message: "获取车主成功",
      });
    } catch (error) {
      res.send({
        code: 1,
        message: "获取车主失败",
      });
    }
  }
}
//获取一个用户的详细信息
async function getUserToOwner(req, res, next) {
  try {
    const { _id } = req.body;
    const data = await Users.findById(_id);
    // console.log(data);
    res.send({
      code: 1,
      message: "获取用户数据成功",
      data,
    });
  } catch (error) {
    console.log("err", error);
  }
}
//修改用户认证状态
async function changeUserToOwner(req, res) {
  try {
    const { _id, checkStatus } = req.body;
    const data = await Users.update({ _id }, { checkStatus });
    res.send({
      code: 1,
      message: "修改数据成功",
      data: 1,
    });
  } catch (error) {
    console.log("err", error);
  }
}
//获取所有用户
async function getUsersCount(req, res, next) {
  try {
    // count: { $sum: 1 }类似于计算数字，_id必须， $match//条件筛选关键词，$group：{}内的是分组条件
    // {$sort://排序关键词 {count:-1//排序规则}}]);
    //aggregate: 统计和计数
    const data1 = await Users.aggregate([
      { $match: { time: { $gte: "2020年1月1日", $lte: "2020年3月31日" } } },
      { $group: { _id: { kind: "$time" }, count: { $sum: 1 } } },
    ]);
    const data2 = await Users.aggregate([
      { $match: { time: { $gte: "2020年4月1日", $lte: "2020年6月30日" } } },
      { $group: { _id: { kind: "$time" }, count: { $sum: 1 } } },
    ]);
    const data3 = await Users.aggregate([
      { $match: { time: { $gte: "2020年7月1月", $lte: "2020年9月30日" } } },
      { $group: { _id: { kind: "$time" }, count: { $sum: 1 } } },
    ]);
    const data4 = await Users.aggregate([
      { $match: { time: { $gte: "2020年10月1日", $lte: "2020年12月31日" } } },
      { $group: { _id: { kind: "$time" }, count: { $sum: 1 } } },
    ]);
    let msg = {};
    data1.forEach((item) => {
      const key = item._id.kind;
      msg[key] = item.count;
    });
    data2.forEach((item) => {
      const key = item._id.kind;
      msg[key] = item.count;
    });
    data3.forEach((item) => {
      const key = item._id.kind;
      msg[key] = item.count;
    });
    data4.forEach((item) => {
      const key = item._id.kind;
      msg[key] = item.count;
    });
    const data = {
      第一季度: data1.length,
      第二季度: data2.length,
      第三季度: data3.length,
      第四季度: data4.length,
    };
    // console.log(data1,data2,data3,data4);
    res.send({
      code: 1,
      data,
    });
  } catch (error) {
    console.log("err", error);
    next(error);
  }
}

//根据Id查找对应车主
async function getCarOwnerId(req, res) {
  const { _id } = req.body;
  console.log(_id);
  const data = await Users.findById({ _id });
  try {
    res.send({
      code: 1,
      data,
      message: "获取车主成功",
    });
  } catch (error) {
    res.send({
      code: 0,
      message: "获取车主失败",
    });
  }
}

//根据状态获取所有待审核的用户
async function getCarOwnerIdCard(req, res) {
  const { checkStatus } = req.body;
  const data = await Users.find({ checkStatus });
  try {
    res.send({
      code: 1,
      data,
      message: "获取成功",
    });
  } catch (error) {
    res.send({
      code: 0,
      message: "获取失败",
    });
  }
}

//审核车主
async function changeOwnerToOwner(req, res) {
  const { _id, role, checkStatus } = req.body;
  console.log(_id, role, checkStatus);
  const data = await Users.findByIdAndUpdate({ _id }, { role, checkStatus });
  console.log(data);
  try {
    res.send({
      code: 1,
      message: "修改成功",
    });
  } catch (error) {
    res.send({
      code: 0,
      message: "修改失败",
    });
  }
}

async function getUsersList(req, res, next) {
  try {
    const { pageSize, pageNumber } = req.body;
    const data = await Users.find()
      .limit(pageSize - 0)
      .skip((pageNumber - 1) * pageSize);
    res.send({ code: 1, data: { data, ...req.body } });
  } catch (error) {
    console.log("err", error);
  }
}

async function getUserToOwner(req, res, next) {
  try {
    const { _id } = req.body;
    const data = await Users.findById(_id);
    // console.log(data);
    res.send({
      code: 1,
      message: "获取用户数据成功",
      data,
    });
  } catch (error) {
    console.log("err", error);
  }
}

async function changeUserToOwner(req, res) {
  try {
    const { _id, checkStatus } = req.body;
    const data = await Users.update({ _id }, { checkStatus });
    res.send({
      code: 1,
      message: "修改数据成功",
      data: 1,
    });
  } catch (error) {
    console.log("err", error);
  }
}

async function getOwnersCount(req, res, next) {
  try {
    const data1 = await Users.find({
      time: { $gte: "2020年1月1日", $lte: "2020年3月31日" },
      role: "1",
    });
    const data2 = await Users.find({
      time: { $gte: "2020年4月1日", $lte: "2020年6月30日" },
      role: "1",
    });
    const data3 = await Users.find({
      time: { $gte: "2020年7月1日", $lte: "2020年9月30日" },
      role: "1",
    });
    const data4 = await Users.find({
      time: { $gte: "2020年10月1日", $lte: "2020年12月31日" },
      role: "1",
    });
    const data = {
      第一季度: data1.length,
      第二季度: data2.length,
      第三季度: data3.length,
      第四季度: data4.length,
    };
    res.send({
      code: 1,
      data,
    });
  } catch (error) {
    console.log("err", error);
    next(error);
  }
}

module.exports = {
  getUsersList,
  changeUserToOwner,
  getUserToOwner,
  getUsersCount,
  getOwnersCount,
  getCarOwnersList,
  getCarOwnerId,
  getCarOwnerIdCard,
  changeOwnerToOwner,
};
